<template>
  <div class="home">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-button  icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        </el-form-item>
        <el-input v-model="inputPro" placeholder="请输入省份名" style="width: 120px"></el-input>
        <el-input v-model="inputCity" placeholder="请输入城市名" style="width: 120px"></el-input>
        <el-button @click="handleSearch">查询</el-button>
        <el-button @click="uploadExcel">Excel导入</el-button>
      </el-form>

    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading">
      <el-table-column
          prop="pro"
          header-align="center"
          align="center"
          label="省份">
      </el-table-column>
      <el-table-column
          prop="city"
          header-align="center"
          align="center"
          label="城市">
      </el-table-column>
      <el-table-column
          prop="nativeRelative"
          header-align="center"
          align="center"
          label="本土新增">
      </el-table-column>
      <el-table-column
          prop="asymptomatic"
          header-align="center"
          align="center"
          label="累计无症状">
      </el-table-column>
      <el-table-column
          prop="asymptomaticRelative"
          header-align="center"
          align="center"
          label="新增无症状">
      </el-table-column>
      <el-table-column
          prop="confirmed"
          header-align="center"
          align="center"
          label="累计确诊">
      </el-table-column>
      <el-table-column
          prop="confirmedRelative"
          header-align="center"
          align="center"
          label="新增确诊">
      </el-table-column>
      <el-table-column
          prop="died"
          header-align="center"
          align="center"
          label="累计死亡">
      </el-table-column>
      <el-table-column
          prop="diedRelative"
          header-align="center"
          align="center"
          label="新增死亡">
      </el-table-column>
      <el-table-column
          prop="curConfirm"
          header-align="center"
          align="center"
          label="现有确诊">
      </el-table-column>
      <el-table-column
          prop="crued"
          header-align="center"
          align="center"
          label="累计治愈">
      </el-table-column>
      <el-table-column
          prop="cruedRelative"
          header-align="center"
          align="center"
          label="新增治愈">
      </el-table-column>
      <el-table-column
          prop="administrativeCode"
          header-align="center"
          align="center"
          label="行政区编码">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          header-align="center"
          align="center"
          label="更新时间">
      </el-table-column>
      <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          label="创建时间">
      </el-table-column>
      <el-table-column
          prop="createBy"
          header-align="center"
          align="center"
          label="创建人">
      </el-table-column>
      <el-table-column
          prop="updateBy"
          header-align="center"
          align="center"
          label="更新人">
      </el-table-column>
      <el-table-column
          header-align="center"
          align="center"
          label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="small" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

        <el-pagination
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                :total="totalPage"
                background
                layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
    <!-- 导入对话框 -->
    <el-dialog
        :title="title"
        :visible.sync="upload.open"
        width="400px"
        append-to-body
    >
    <el-upload
        class="upload-demo"
        drag
        accept=".xlsx, .xls"
        action="api/datachinacity/import"

        >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">仅允许导入xls、xlsx格式文件。</div>
    </el-upload>
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitFileForm">确 定</el-button>-->
<!--        <el-button @click="upload.open = false">取 消</el-button>-->
<!--      </div>-->
    </el-dialog>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  // import {fetchList, delObj} from '@/api/datachinacity'
  import TableForm from './datachinacity-form'
  import loginRequest from "../../utils/loginRequest";
  import request from "../../utils/request";
  // import {mapGetters} from 'vuex'
  export default {
    //todo 文件上传需要提供模板
    data () {
      return {
        title: '导入对话框',
        upload: {
          open: false
        },
        inputCity: null,
        inputPro: null,
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm
    },
    created () {
      this.getDataList()
      // console.log("我出现了！")
    },
    computed: {
      // ...mapGetters(['permissions'])
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        if (this.inputCity === ""){
          request.get('/datachinacity/page',{current :this.pageIndex,size: this.pageSize}).then(res=>{
            this.dataList = res.result.records
            this.totalPage = res.result.total
            this.pageIndex = res.result.current
          })
        }
        else {
          request.get('/datachinacity/page',{current :this.pageIndex,size: this.pageSize, city: this.inputCity}).then(res=>{
            this.dataList = res.result.records
            this.totalPage = res.result.total
            this.pageIndex = res.result.current
          })

        }

        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.handleSearch()
      },
      // 当前页
      currentChangeHandle (val) {
        //传入当前点击的页码
        this.pageIndex = val
        this.handleSearch()
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          // return delObj(id)
          return  request.delet('/datachinacity/' + id)

        }).then(data => {
          this.$message.success('删除成功')
          this.handleSearch()
        }).catch(() => {})
      },
      handleSearch(){
        //以这个Vue的input的值作为参数，发送Ajax请求
        this.dataListLoading = true
        if (this.inputPro === ''){
          request.get('/datachinacity/page',{current :this.pageIndex,size: this.pageSize, city: this.inputCity}).then(res=>{
            this.dataList = res.result.records
            this.totalPage = res.result.total
            this.pageIndex = res.result.current
          })
        }
        else if (this.inputCity === ''){
          request.get('/datachinacity/page',{current :this.pageIndex,size: this.pageSize,  pro: this.inputPro}).then(res=>{
            this.dataList = res.result.records
            this.totalPage = res.result.total
            this.pageIndex = res.result.current
          })
        }
        else if (this.inputPro === '' && this.inputCity === ""){
          request.get('/datachinacity/page',{current :this.pageIndex,size: this.pageSize}).then(res=>{
            this.dataList = res.result.records
            this.totalPage = res.result.total
            this.pageIndex = res.result.current
          })
        }
        else {
          request.get('/datachinacity/page',{current :this.pageIndex,size: this.pageSize, city: this.inputCity, pro: this.inputPro}).then(res=>{
            this.dataList = res.result.records
            this.totalPage = res.result.total
            this.pageIndex = res.result.current
          })
        }

        this.dataListLoading = false
      },
      uploadExcel(){
        //原来设计是：设个函数有个param参数，参数就是文件
        // loginRequest.post("/datachinacity/import",param.file)
        this.upload.open = true
      }

    }
  }
</script>
